package org.hot100_3;

import java.util.ArrayList;
import java.util.List;

public class leetcode39 {

    List<List<Integer>> res= new ArrayList<>();
    List<Integer> item = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {

        backtracking(candidates,target,0,0);

        return res;
    }

    private void  backtracking(int [] candidates,int target,int sum,int startIndex){

        if (sum>target){
            return;
        }

        if (sum==target){
            res.add(new ArrayList<>(item));
            return;
        }

        for (int i = startIndex; i < candidates.length; i++) {
            sum += candidates[i];
            item.add(candidates[i]);
            backtracking(candidates,target,sum,i);
            item.remove(item.size()-1);
            sum -= candidates[i];
        }

    }
}
